package cn.tedu.service.impl;

import cn.tedu.constant.WxConstant;
import cn.tedu.entity.TextMsg;
import cn.tedu.service.IMessageHandler;
import cn.tedu.service.IWxService;
import me.chanjar.weixin.common.api.WxConsts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Kiven
 */
@Service(WxConsts.XmlMsgType.VOICE)
public class VoiceMessageHandler implements IMessageHandler {

    @Value("${wx.temporaryMaterialFilePath}")
    private String temporaryMaterialFilePath;

    @Autowired
    private IWxService wxService;
    @Override
    public String handler(Map<String, Object> map) {
        // 收到的语音消息是
        String voiceContent =(String) map.get(WxConstant.VOICE_XML_MSG_RECOGNITION);
        System.out.println("content:"+voiceContent);
        // TODO 获取用户发的语音信息内容暂且失败，开启后需要24小时
        //try {
        //    //File file = WeiXinUtil.getFile(WxUrlConstant.TEMPORARY_MATERIAL.replace(WxConstant.DX_ACCESS_TOKEN, wxService.getToken())
        //    //        .replace(WxConstant.MEDIA_ID, (String) map.get(WxConstant.XML_MEDIA_ID)),temporaryMaterialFilePath);
        //    String mediaId = (String) map.get(WxConstant.XML_MEDIA_ID);
        //    System.out.println("mediaid:"+mediaId);
        //    String token = wxService.getToken();
        //    System.out.println("token:"+token);
        //    String url = WxUrlConstant.TEMPORARY_MATERIAL.replace(WxConstant.DX_ACCESS_TOKEN,token)
        //                    .replace(WxConstant.MEDIA_ID, mediaId);
        //    System.out.println("url:"+url);
        //    String s = HttpClientUtil.doGet(url);
        //    System.out.println("返回值："+s);
        //
        //} catch (Exception e) {
        //    e.printStackTrace();
        //    throw new RuntimeException(e);
        //}


        return TextMsg.ofSendMsg(map,"Nice Voice").toXml();
    }
}
